﻿using System;
using System.Linq;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.DAL
{
    public class ItemCategoryDa
    {
        public static int Delete(int id)
        {
            try
            {
                using (var db = new DatabaseEntities())
                {
                    var model = db.ItemCategories.Find(id);
                    if (model != null)
                    {
                        db.ItemCategories.Remove(model);
                        return db.SaveChanges();
                    }
                }
            }
            catch (Exception)
            {

                return 0;
            }
            return 0;
        }

        /// <summary>
        /// get name by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetName(int? id)
        {
            using (var db = new DatabaseEntities())
            {
                var model = db.ItemCategories.Find(id);
                if (model != null)
                {
                    return model.Name;
                }
                return null;
            }
        }

        /// <summary>
        /// count foreign ref
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int CountRef(int id)
        {
            using (var db = new DatabaseEntities())
            {
                return db.Items.Count(x => x.ItemCategoriesId == id);
            }
        }
    }
}
